package logic

import (
	"context"

	"wxapi/internal/services"
	"wxapi/internal/svc"
	"wxapi/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetDoctorOrderEvaluateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetDoctorOrderEvaluateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetDoctorOrderEvaluateLogic {
	return &GetDoctorOrderEvaluateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetDoctorOrderEvaluateLogic) GetDoctorOrderEvaluate(req *types.GetDoctorOrderEvaluateReq) (resp *types.GetDoctorOrderEvaluateResp, err error) {
	list, err := services.GetEvaluatesByDoctorId(l.ctx, l.svcCtx, req.DoctorID, req.Page, req.PageSize)
	if err != nil {
		return nil, err
	}

	resp = &types.GetDoctorOrderEvaluateResp{
		List: make([]types.OrderEvaluate, 0),
	}
	for _, item := range list {
		evaluate := types.OrderEvaluate{
			ID:         item.ID,
			Point:      item.Point,
			Content:    item.Content,
			CreateTime: item.CreatedAt.ToDateTimeString(),
			Images:     make([]types.OrderEvaluateImage, 0),
		}
		if item.User != nil {
			evaluate.UserName = item.User.Nickname
			if item.User.UserInfo != nil {
				if item.User.UserInfo.UserName != "" {
					evaluate.UserName = item.User.UserInfo.UserName
				}
				if item.User.UserInfo.Phone != "" {
					evaluate.UserPhone = item.User.UserInfo.Phone[:3] + "****" + item.User.UserInfo.Phone[7:]
				}
			}
		} else {
			evaluate.UserName = "匿名用户"
		}
		for _, image := range item.EvaluateImage {
			evaluate.Images = append(evaluate.Images, types.OrderEvaluateImage{
				ID:   image.ID,
				Path: image.Path,
			})
		}
		resp.List = append(resp.List, evaluate)
	}
	return
}
